package com.example.springbootlocal.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootlocal.common.Result;
import com.example.springbootlocal.system.domain.Log;
import com.example.springbootlocal.system.domain.vo.PageVo;
import com.example.springbootlocal.system.service.LogService;
import com.example.springbootlocal.utils.page.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author X.I.O
 * @title: LogController
 * @projectName spring-boot-local
 * @description: TODO
 * @date 2021/11/11 15:26
 */
@Api(tags = {"日志服务"})
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogService logService;

    @ApiOperation("日志分页测试")
    @PostMapping("page")
    public JSONObject logPageController(@RequestBody PageVo<Log> pageVo){
        IPage<Log> logIPage=logService.getLogPage(pageVo);
        return Result.successInfo("success",logIPage);
    }

    @ApiOperation("日志分页测试")
    @PostMapping("page2")
    public JSONObject logPageController(@ApiParam("页码") @RequestParam(value = "page",defaultValue = "1")Integer page,
                                        @ApiParam("条数")@RequestParam(value = "size",defaultValue = "10")Integer size,
                                        @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        IPage<Log> logIPage=logService.getLogPage(page,size,keyword);
        return Result.successInfo("success",logIPage);
    }

    @ApiOperation("新增10000条数据")
    @PostMapping("save")
    public JSONObject addController(){
        Log log=logService.getLogById(0);
        for (int i = 0; i < 10000; i++) {
            logService.saveLog(log);
        }
        return Result.successInfo();
    }

    @ApiOperation("新增10000条测试2")
    @PostMapping("save2")
    public JSONObject addController2(){
        List<Log> logList=new ArrayList<>(10000);
        for (int i = 0; i < 10000; i++) {
            Log log1=new Log();
            log1.setUrl("测试url");
            log1.setParams("测试参数");
            log1.setLogName("测试名称");
            log1.setClazz("测试clazz");
            logList.add(log1);
        }
        logService.saveList(logList);
        return Result.successInfo();

    }
}
